package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"zkzk2/models/mysql"
	"zkzk2/pkg"

	"zkzk2/zk2rpc/internal/svc"
	"zkzk2/zk2rpc/zk2rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *zk2rpc.RegisterRequest) (*zk2rpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	//captcha := redis.Client.Get("captcha" + in.Tel)
	//if captcha.Val() != in.Captcha {
	//	return nil, fmt.Errorf("图片未验证,请重新输入")
	//}
	//sendsms := redis.Client.Get("sendsms" + in.Tel + in.Source)
	//if sendsms.Val() != in.Sendsms {
	//	return nil, fmt.Errorf("短信未验证,请重新输入")
	//}
	register := mysql.User{
		Model:    gorm.Model{},
		Tel:      in.Tel,
		Password: pkg.Md5(in.Password),
	}
	mysql.DB.Debug().Model(&register).Create(&register)
	if register.ID == 0 {
		return nil, fmt.Errorf("账号已存在,请重新输入")
	}

	return &zk2rpc.RegisterResponse{Id: int64(register.ID)}, nil
}
